<template>
  <div class="article-manager">
    <div class="i-layout-page-header">
      <div class="i-layout-page-header">
        <span class="ivu-page-header-title">诊疗记录</span>
        <div>
          <Tabs v-model="currentTab" @on-click="changeTab">
            <TabPane
              :label="item.label"
              :name="item.value.toString()"
              v-for="(item, index) in headerList"
              :key="index"
            />
          </Tabs>
        </div>
      </div>
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt fromBox" v-if="currentIndex==0">
      <Table
        :columns="columns"
        :data="tableData"
        ref="table"
        :loading="loading"
        highlight-row
        no-data-text="暂无数据"
        no-filtered-data-text="暂无筛选结果"
        @on-select="handleSelectRow"
        @on-select-cancel="handleCancelRow"
        @on-select-all="handleSelectAll"
        @on-select-all-cancel="handleSelectAll"
        class="orderData mt25"
      >
        <template slot-scope="{ row, index }" slot="id">
          {{index+1}}
        </template>
        <template slot-scope="{ row, index }" slot="no">
          <span>{{row.no}}号</span>
        </template>
		<template slot-scope="{ row, index }" slot="status">
		  <span v-if="row.status == 0">待支付</span>
		  <span v-else-if="row.status == 1">待就诊</span>
		  <span v-else-if="row.status == 2">就诊中</span>
		  <span v-else-if="row.status == 3">已看诊</span>
		  <span v-else-if="row.status == 4">已过号</span>
		  <span v-else>已取消</span>
		</template>
        <template slot-scope="{ row, index }" slot="action">
          <a  @click="showCheck(row)">预检详情</a>
        </template>
      </Table>
      <div class="acea-row row-right page">
        <Page
          :total="page.total"
          :current="page.pageNum"
          show-elevator
          show-total
          @on-change="pageChange"
          :page-size="page.pageSize"
          @on-page-size-change="limitChange"
        />
      </div>
    </Card>
	
	<Card :bordered="false" dis-hover class="ivu-mt fromBox" v-else>
		<Table
		  :columns="columns2"
		  :data="tableData2"
		  ref="table"
		  :loading="loading2"
		  highlight-row
		  no-data-text="暂无数据"
		  no-filtered-data-text="暂无筛选结果"
		  @on-select="handleSelectRow2"
		  @on-select-cancel="handleCancelRow2"
		  @on-select-all="handleSelectAll2"
		  @on-select-all-cancel="handleSelectAll2"
		  class="orderData mt25"
		>
		  <template slot-scope="{ row, index }" slot="id">
		    {{index+1}}
		  </template>
		  <template slot-scope="{ row, index }" slot="history">
		   <span v-if="row.jbs">疾病史：否<br /></span>
		   <span v-else>疾病史：{{row.jbs_text}}<br /></span>
		   <span v-if="row.bms">过敏史：否<br /></span>
		   <span v-else>过敏史：{{row.bms_text}}<br /></span>
		   <span v-if="row.sss">手术史：否<br /></span>
		   <span v-else>手术史：{{row.sss_text}}<br /></span>
		   <span v-if="row.jzs">家族史：否<br /></span>
		   <span v-else>家族史：{{row.jzs_text}}<br /></span>
		   <span v-if="row.qt">其他：{{row.qt}}<br /></span>
		  </template>
		  
		  <template slot-scope="{ row, index }" slot="programme">
		   <template v-for="it in row.programme.split(',')">
			   <span v-if="it == '1'">普通单光镜,</span>
			   <span v-if="it == '2'">棱镜,</span>
			   <span v-if="it == '3'">渐进多焦镜,</span>
			   <span v-if="it == '4'">近视离焦镜,</span>
			   <span v-if="it == '5'">成长乐,</span>
			   <span v-if="it == '6'">美德可儿,</span>
			   <span v-if="it == '7'">大直径RGP,</span>
			   <span v-if="it == '8'">多焦点软镜,</span>
			   <span v-if="it == '9'">软镜,</span>
			   <span v-if="it == '10'">CME减负镜</span>
		   </template>
		  </template>
		  
		  <template slot-scope="{ row, index }" slot="action">
		    <a  @click="showShow(row)">看诊详情</a>
		  </template>
		</Table>
		<div class="acea-row row-right page">
		  <Page
		    :total="page2.total"
		    :current="page2.pageNum"
		    show-elevator
		    show-total
		    @on-change="pageChange2"
		    :page-size="page.pageSize2"
		    @on-page-size-change="limitChange2"
		  />
		</div>
	</Card>
    <check-form
      :formValidate="formValidate"
      :titleFrom="titleFrom"
      @getList="getList"
      @selectRule="selectRule"
      ref="checkForm"
      @clearFrom="clearFrom"
    ></check-form>
    <media-form
      :formValidate="formValidate"
      :titleFrom="titleFrom"
      @getList="getList"
      @selectRule="selectRule"
      ref="mediaForm"
      @clearFrom="clearFrom"
    ></media-form>
  </div>
</template>

<script>
import formCreate from '@form-create/iview';
import { dataFromApi } from '@/api/setting';
import request from '@/libs/request';
import { getLogo } from '@/api/common';
import { ListUserFinishedApi} from '@/api/register';
import { ListApi} from '@/api/mehistory';
import checkForm from './components/checkForm'; 
import mediaForm from './components/mediaForm'; 

export default {
  name: 'setting_setSystem',
  components: { checkForm,mediaForm,formCreate: formCreate.$form() },
  data() {
    return {
		formValidate: {},
		titleFrom: '',
      rules: [],
      option: {
        form: {
          labelWidth: 185,
        },
        submitBtn: {
          col: {
            span: 3,
            push: 3,
          },
        },
        global: {
          upload: {
            props: {
              onSuccess(res, file) {
                if (res.status === 200) {
                  file.url = res.data.src;
                } else {
                  this.$Message.error(res.msg);
                }
              },
            },
          },
          frame: {
            props: {
              closeBtn: false,
              okBtn: false,
            },
          },
        },
      },
      spinShow: false,
      FromData: null,
      currentTab: '',
      headerList: [{
		  'label':'预检',
		  'value':0
	  },{
		  'label':'病历',
		  'value':1
	  }],
      headerChildrenList: [],
      childrenId: '',
      title: '',
	  loading: false,
	  tableData: [],
	  columns: [
		  {
		    title: '序号',
		    align: 'center',
		    slot: 'id',
		    width: 80,
		  },
		  {
		    title: '患者',
		    align: 'center',
		    key: 'user_name',
		    width: 80,
		  },
		  {
		    title: '就诊事项',
		    align: 'center',
		    key: 'sub_item_name',
		    width: 100,
		  },
		  {
		    title: '医生',
		    align: 'center',
		    key: 'doc_name',
		    width: 80,
		  },
		  {
		    title: '挂号时间',
		    align: 'center',
		    key: 'created_time',
		    width: 150,
		  },
		  {
		    title: '就诊时间',
		    align: 'center',
		    key: 'see_time',
		    width: 150,
		  },
		  {
		    title: '预约编号',
		    align: 'center',
		    slot: 'no',
		    width: 100,
		  },
		  {
		    title: '就诊室',
		    align: 'center',
		    key: 'room_name',
		    width: 150,
		  },
		  {
		    title: '挂号费用',
		    align: 'center',
		    key: 'amount',
		    width: 80,
		  },
		  {
		    title: '状态',
		    align: 'center',
		    slot: 'status',
		    width: 100,
		  },
		  {
		    title: '操作',
		    slot: 'action',
		    fixed: 'right',
		    width: 100,
		    align: 'center',
		  },
	  ],
	  page: {
	    total: 0, // 总条数
	    pageNum: 1, // 当前页
	    pageSize: 10, // 每页显示条数
	  },
	  currentIndex:0,
	  columns2: [
	  	{
	      title: '序号',
	      align: 'center',
	      slot: 'id',
	      width: 100,
	    },
	  	{
	      title: '医生',
	      align: 'center',
	      key: 'doc_name',
	      width: 100,
	    },
	  	{
	      title: '主诉',
	      align: 'center',
	      key: 'main_chinf',
	      width: 100,
	    },
	  	{
	      title: '现病史',
	      align: 'center',
	      key: 'hpi',
	      width: 300,
	    },
	  	{
	      title: '即往史',
	      align: 'center',
	      slot: 'history',
	      width: 200,
	    },
	  	{
	      title: '专科检查',
	      align: 'center',
	      key: 'history',
	      width: 200,
	    },
	  	// {
	   //    title: '初步诊断',
	   //    align: 'center',
	   //    key: 'primary_diagnosis',
	   //    width: 200,
	   //  },
	  	{
	      title: '处理意见',
	      align: 'center',
	      key: 'handle_advise',
	      width: 200,
	    },
	  	{
	      title: '矫正方案',
	      align: 'center',
	      slot: 'programme',
	      width: 100,
	    },
	    {
	      title: '操作',
	      slot: 'action',
	      fixed: 'right',
	      width: 100,
	      align: 'center',
	    },
	  ],
	  page2: {
	    total: 0, // 总条数
	    pageNum: 1, // 当前页
	    pageSize: 10, // 每页显示条数
	  },
	  loading2: false,
	  tableData2: [],
    };
  },
  created() {
    // this.getAllData();
	this.loadUserCheckList();
  },
  watch: {
    $route(to, from) {
      this.headerChildrenList = [];
      this.getAllData();
    },
    childrenId() {
      this.getFrom();
    },
  },
  methods: {
	  //获取预检记录 
	loadUserCheckList(){
		this.loading = true;
		ListUserFinishedApi({
		  page: this.page.pageNum,
		  limit: this.page.pageSize,
			uid:this.$route.params.id
		})
		  .then(async (res) => {
		    let data = res.data;
		    this.tableData = data.list;
		    this.page.total = data.count;
		    this.loading = false;
		  })
		  .catch((res) => {
		    this.loading = false;
		    this.$Message.error(res.msg);
		  });
	},
	  //获取即往病历记录 
	loadUserHistoryList(){
		this.loading2 = true;
		ListApi({
		  page: this.page2.pageNum,
		  limit: this.page2.pageSize,
			uid:this.$route.params.id
		})
		  .then(async (res) => {
		    let data = res.data;
		    this.tableData2 = data.list;
		    this.page2.total = data.count;
		    this.loading2 = false;
		  })
		  .catch((res) => {
		    this.loading2 = false;
		    this.$Message.error(res.msg);
		  });
	},
	
    childrenList(index) {
      let that = this;
      that.headerList.forEach(function (item) {
        if (item.value.toString() === that.currentTab) {
          if (item.children === undefined) {
            that.childrenId = item.id;
            that.headerChildrenList = [];
          } else {
            that.headerChildrenList = item.children;
            that.childrenId = item.children.length ? item.children[index ? index : 0].id.toString() : '';
          }
        }
      });
    },
    // 头部tab
    getHeader(index) {
      this.spinShow = true;
      return new Promise((resolve, reject) => {
        let tab_id = this.$route.params.tab_id;
        let data = {
          type: this.$route.params.type ? this.$route.params.type : 0,
          pid: tab_id ? tab_id : 0,
        };
        ListUserFinishedApi(data)
          .then(async (res) => {
            let config = res.data.config_tab;
            this.headerList = config;
            this.currentTab = config[index ? index : 0].value.toString();
            this.childrenList(index ? 1 : 0);
            resolve(this.currentTab);
            this.spinShow = false;
          })
          .catch((err) => {
            this.spinShow = false;
            this.$Message.error(err);
          });
      });
    },
    // 表单
    getFrom() {
      this.spinShow = true;
      return new Promise((resolve, reject) => {
        let ids = '';
        if (this.$route.params.type === '3') {
          ids = this.$route.params.tab_id;
        } else {
          if (this.childrenId) {
            ids = this.childrenId;
          } else {
            ids = this.currentTab;
          }
        }
        let data = {
          tab_id: Number(ids),
        };
        let logistics = 'freight/config/edit_basics',
          agent = 'agent/config/edit_basics',
          integral = 'marketing/integral_config/edit_basics',
          sms = 'serve/sms_config/edit_basics',
          config = 'setting/config/edit_basics';
        let url =
          this.$route.name === 'setting_logistics'
            ? logistics
            : this.$route.name === 'setting_distributionSet'
            ? agent
            : this.$route.name === 'setting_message'
            ? sms
            : this.$route.name === 'setting_setSystem'
            ? config
            : integral;
        dataFromApi(data, url)
          .then(async (res) => {
            this.spinShow = false;
            if (res.data.status === false) {
              return this.$authLapse(res.data);
            }
            this.FromData = res.data;
            this.rules = res.data.rules;
            this.title = res.data.title;
          })
          .catch((res) => {
            this.spinShow = false;
            this.$Message.error(res.msg);
          });
      });
    },
    async getAllData() {
		
    },
    // 选择
    changeTab(e) {
		if(e == 0){
			this.loadUserCheckList();
		}else{
			this.loadUserHistoryList();
		}
		this.currentIndex = e;
	  console.info(e);
    },
    // 二级选择
    changeChildrenTab(name) {
      this.childrenId = name;
    },
	//  选中某一行
	handleSelectRow(selection, row) {
	  
	},
	//  取消某一行
	handleCancelRow(selection, row) {
		
	},
	//全选和取消全选时触发
	handleSelectAll(selection) {
	  
	},
	
	pageChange(index) {
	  this.page.pageNum = index;
	  this.loadUserCheckList();
	},
	limitChange(limit) {
	  this.page.pageSize = limit;
	  this.loadUserCheckList();
	},
	//  选中某一行
	handleSelectRow2(selection, row) {
	  
	},
	//  取消某一行
	handleCancelRow2(selection, row) {
		
	},
	//全选和取消全选时触发
	handleSelectAll2(selection) {
	  
	},
	
	pageChange2(index) {
	  this.page.pageNum = index;
	  this.loadUserCheckList();
	},
	limitChange2(limit) {
	  this.page.pageSize = limit;
	  this.loadUserCheckList();
	},
	showCheck(row){
		this.formValidate = {
			yjjc_qj_right:row.record.yjjc_qj_right,
			yjjc_zj_right:row.record.yjjc_zj_right,
			yjjc_zw_right:row.record.yjjc_zw_right,
			yjjc_qj_left:row.record.yjjc_qj_left,
			yjjc_zj_left:row.record.yjjc_zj_left,
			yjjc_zw_left:row.record.yjjc_zw_left,
			zjyg_qj_right:row.record.zjyg_qj_right,
			zjyg_zj_right:row.record.zjyg_zj_right,
			zjyg_zw_right:row.record.zjyg_zw_right,
			zjyg_jzsl_right:row.record.zjyg_jzsl_right,
			zjyg_xjg_right:row.record.zjyg_xjg_right,
			zjyg_lj_right:row.record.zjyg_lj_right,
			zjyg_qj_left:row.record.zjyg_qj_left,
			zjyg_zj_left:row.record.zjyg_zj_left,
			zjyg_zw_left:row.record.zjyg_zw_left,
			zjyg_jzsl_left:row.record.zjyg_jzsl_left,
			zjyg_xjg_left:row.record.zjyg_xjg_left,
			zjyg_lj_left:row.record.zjyg_lj_left,
			yjsl_od_sc:row.record.yjsl_od_sc,
			yjsl_os_sc:row.record.yjsl_os_sc,
			yjsl_od_cc:row.record.yjsl_od_cc,
			yjsl_os_cc:row.record.yjsl_os_cc,
			jjsl_od_sc:row.record.jjsl_od_sc,
			jjsl_os_sc:row.record.jjsl_os_sc,
			jjsl_od_cc:row.record.jjsl_od_cc,
			jjsl_os_cc:row.record.jjsl_os_cc,
			lts:row.record.lts,
			cyb:row.record.cyb,
			sj:row.record.sj,
			remark:row.record.remark,
			ktyg_qj_right:row.record.ktyg_qj_right,
			ktyg_zj_right:row.record.ktyg_zj_right,
			ktyg_zw_right:row.record.ktyg_zw_right,
			ktyg_jzsl_right:row.record.ktyg_jzsl_right,
			ktyg_else_right:row.record.ktyg_else_right,
			ktyg_qj_left:row.record.ktyg_qj_left,
			ktyg_zj_left:row.record.ktyg_zj_left,
			ktyg_zw_left:row.record.ktyg_zw_left,
			ktyg_jzsl_left:row.record.ktyg_jzsl_left,
			ktyg_else_left:row.record.ktyg_else_left,
			ktyg_status:row.record.ktyg_status,
			ys_od:row.record.ys_od,
			ys_os:row.record.ys_os,
			yzcd_od:row.record.yzcd_od,
			yzcd_os:row.record.yzcd_os,
			pk_od:row.record.pk_od,
			pk_os:row.record.pk_os,
			tk_od:row.record.tk_od,
			tk_os:row.record.tk_os,
			jmsg_od:row.record.jmsg_od,
			jmsg_os:row.record.jmsg_os,
		};
		this.$refs.checkForm.modals = true;
		this.$refs.checkForm.valids = false;
		
		this.titleFrom = '预检详情';
	},
	showShow(row){
		this.formValidate = {
			id:row.id,
			arc_id:row.uid,
			doc_uid:row.doc_uid,
			reg_id:row.reg_id,
			no_id:row.no_id,
			no:row.no,
			main_chinf:row.main_chinf,
			hpi:row.hpi,
			jbs:row.jbs==1?true:false,
			jbs_text:row.jbs_text,
			bms:row.bms==1?true:false,
			bms_text:row.bms_text,
			sss:row.sss==1?true:false,
			sss_text:row.sss_text,
			jzs:row.jzs==1?true:false,
			jzs_text:row.jzs_text,
			qt:row.qt,
			mpi:row.mpi,
			special_check:row.special_check,
			primary_diagnosis:row.primary_diagnosis,
			handle_advise:row.handle_advise,
			programme:row.programme.split(',').map(Number),
			check_count:row.check_count
		};
		this.$refs.mediaForm.modals = true;
		this.$refs.mediaForm.valids = false;
		
		this.titleFrom = '看诊详情';
	}
  },
};
</script>

<style scoped lang="stylus">
.ivu-tabs {
  margin-bottom: 18px;
}

.fromBox {
  min-height: 600px;
}
</style>
